package com.xk.school_device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xk.school_device.common.Result;
import com.xk.school_device.entity.Disposals;
import com.xk.school_device.entity.Devices;
import com.xk.school_device.service.IDisposalsService;
import com.xk.school_device.service.IDevicesService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 设备报废控制器
 * </p>
 *
 * @author xk
 * @since 2025-04-16
 */
@RestController
@RequestMapping("/api/disposals")
public class DisposalsController {

    @Resource
    private IDisposalsService disposalsService;

    @Resource
    private IDevicesService devicesService;

    /**
     * 添加报废记录
     */
    @PostMapping("/add")
    public Result add(@RequestBody Disposals disposal) {
        disposal.setCreatedAt(LocalDateTime.now());

        // 更新设备状态为报废
        Devices device = devicesService.getByIdDevice(disposal.getDeviceId());
        if (device != null) {
            device.setStatus("disposed");
            devicesService.updateByIdDisposed(device);
        }

        disposalsService.save(disposal);
        return Result.success("设备报废记录添加成功");
    }

    /**
     * 查询报废记录列表
     */
    @GetMapping("/list")
    public Result list() {
        List<Disposals> list = disposalsService.list();
        return Result.success(list);
    }

    /**
     * 修改报废记录
     */
    @PutMapping("/update")
    public Result update(@RequestBody Disposals disposal) {
        disposalsService.updateById(disposal);
        return Result.success("报废记录更新成功");
    }

    /**
     * 删除报废记录
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        disposalsService.removeById(id);
        return Result.success("报废记录删除成功");
    }

    /**
     * 获取单个报废记录
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable String id) {
        Disposals disposal = disposalsService.getById(id);
        return Result.success(disposal);
    }
}
